Public Class frmManagePrivilege

#Region "Declare Variables"
    Private m_eScreenMode As eScreenMode
    Private m_Privilege As New PoliceHealthClass.Privilege
    Private m_ManagePrivilegeControl As New PoliceHealthClass.ManagePrivilegeControl
    Private IsPostBack As Boolean = False

    Private cbbStoolExamResultHistory As ComboBox

    Delegate Sub delLoadDatagridviewPrivilege(ByVal intSelectRowIndex As Integer)

#End Region

#Region "Enumuration"
    Private Enum eScreenMode As Integer
        Initial
        View
        Add
        Edit
    End Enum
#End Region

#Region "General Functions"
    Private Sub SetScreenMode(ByVal ScreenMode As eScreenMode)
        m_eScreenMode = ScreenMode
        Select Case ScreenMode
            Case eScreenMode.Initial
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, btnDelete, btnEdit, txtPrivilegeNameTha, txtPrivilegeNameEng, txtPrivilegeDescription)
                PoliceHealthClass.Utilities.EnableControl(True, btnAdd)

                dgvPrivilege.Enabled = True

                If Not Me.IsPostBack Then

                    ConnectionString.ShowLoadingForm()
                    Me.Invoke(New delLoadDatagridviewPrivilege(AddressOf LoadDatagridviewPrivilege), 0)
                    ConnectionString.TerminateLoadingForm()

                    'LoadDatagridviewPrivilege()
                    Me.IsPostBack = True
                End If

            Case eScreenMode.View
                PoliceHealthClass.Utilities.EnableControl(False, btnSave, btnCancel, txtPrivilegeNameTha, txtPrivilegeNameEng, txtPrivilegeDescription)
                PoliceHealthClass.Utilities.EnableControl(True, btnEdit, btnDelete, btnAdd)

                dgvPrivilege.Enabled = True
            Case eScreenMode.Add
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtPrivilegeNameTha, txtPrivilegeNameEng, txtPrivilegeDescription)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)
                ClearFormData()

                dgvPrivilege.Enabled = False

            Case eScreenMode.Edit
                PoliceHealthClass.Utilities.EnableControl(True, btnSave, btnCancel, txtPrivilegeNameTha, txtPrivilegeNameEng, txtPrivilegeDescription)
                PoliceHealthClass.Utilities.EnableControl(False, btnEdit, btnDelete, btnAdd)

                dgvPrivilege.Enabled = False
        End Select


    End Sub


    Private Function VerifyInputData() As Boolean
        Return m_ManagePrivilegeControl.verifyInsertPrivilege()
    End Function

    Private Sub LoadDatagridviewPrivilege(Optional ByVal intSelectRowIndex As Integer = 0)
        dgvPrivilege.Enabled = True
        m_Privilege.LoadPrivilege()

        Dim intRowCount As Integer = 0
        dgvPrivilege.Rows.Clear()
        While m_Privilege.NextRecord
            dgvPrivilege.Rows.Add()
            dgvPrivilege.Rows(intRowCount).Cells("Order").Value = intRowCount + 1
            dgvPrivilege.Rows(intRowCount).Cells("PrivilegeID").Value = m_Privilege.privilege_id
            dgvPrivilege.Rows(intRowCount).Cells("PrivilegeNameThai").Value = m_Privilege.privilege_name_tha
            dgvPrivilege.Rows(intRowCount).Cells("PrivilegeNameEng").Value = m_Privilege.privilege_name_eng
            dgvPrivilege.Rows(intRowCount).Cells("PrivilegeDescription").Value = m_Privilege.description


            dgvPrivilege.Rows(intRowCount).Selected = False

            intRowCount += 1
        End While

        If intSelectRowIndex < dgvPrivilege.Rows.Count Then
            dgvPrivilege.Rows(intSelectRowIndex).Selected = True
            dgvPrivilege.CurrentCell = dgvPrivilege.Rows(intSelectRowIndex).Cells(0)
        End If

        If dgvPrivilege.Rows.Count > 0 Then
            SetScreenMode(eScreenMode.View)
            LoadDataForm(dgvPrivilege.Rows(intSelectRowIndex).Cells("PrivilegeID").Value)
        Else
            If Me.IsPostBack Then
                SetScreenMode(eScreenMode.Initial)
            End If
            ClearFormData()
        End If
    End Sub

    Private Sub LoadDataForm(ByVal PrivilegeID As String)
        txtPrivilegeID.Text = PrivilegeID

        'Load Form
        If dgvPrivilege.SelectedRows.Count > 0 Then
            txtPrivilegeNameTha.Text = dgvPrivilege.SelectedRows(0).Cells("PrivilegeNameThai").Value
            txtPrivilegeNameEng.Text = dgvPrivilege.SelectedRows(0).Cells("PrivilegeNameEng").Value
            txtPrivilegeDescription.Text = dgvPrivilege.SelectedRows(0).Cells("PrivilegeDescription").Value
        Else
            txtPrivilegeNameTha.Text = dgvPrivilege.Rows(0).Cells("PrivilegeNameThai").Value
            txtPrivilegeNameEng.Text = dgvPrivilege.Rows(0).Cells("PrivilegeNameEng").Value
            txtPrivilegeDescription.Text = dgvPrivilege.Rows(0).Cells("PrivilegeDescription").Value
        End If

    End Sub

    Private Sub ClearFormData()
        txtPrivilegeID.Text = ""
        txtPrivilegeNameTha.Text = ""
        txtPrivilegeNameEng.Text = ""
        txtPrivilegeDescription.Text = ""
        PoliceHealthClass.Utilities.EnableControl(False, btnEdit)
    End Sub






#End Region

#Region "Event Handling Functions"
    Private Sub frmPolice_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'CMU_KPI_PHASE_1DataSet.tbError_Logging' table. You can move, or remove it, as needed.
        SetScreenMode(eScreenMode.Initial)
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        If dgvPrivilege.SelectedRows.Count > 0 Then
            LoadDataForm(dgvPrivilege.Rows(0).Cells("PrivilegeID").Value)
            SetScreenMode(eScreenMode.View)
        Else
            SetScreenMode(eScreenMode.Initial)
        End If
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim SelectIndex As Integer = 0
        Dim blVerifyInputControl As Boolean = False


        If m_eScreenMode = eScreenMode.Edit OrElse m_eScreenMode = eScreenMode.Add Then
            If VerifyInputData() Then

                If dgvPrivilege.SelectedRows.Count > 0 Then
                    SelectIndex = dgvPrivilege.SelectedRows(0).Index
                End If


                m_Privilege.privilege_id = PoliceHealthClass.Utilities.ClearEmptyToMinValue(txtPrivilegeID.Text, TypeCode.Int32)
                m_Privilege.privilege_name_tha = txtPrivilegeNameTha.Text
                m_Privilege.privilege_name_eng = txtPrivilegeNameEng.Text
                m_Privilege.description = txtPrivilegeDescription.Text



                m_ManagePrivilegeControl.showInsertResult(m_Privilege.insertPrivilege(m_Privilege))

                ConnectionString.ShowLoadingForm()
                Me.Invoke(New delLoadDatagridviewPrivilege(AddressOf LoadDatagridviewPrivilege), dgvPrivilege.SelectedRows(0).Index)
                ConnectionString.TerminateLoadingForm()

                'LoadDatagridviewPrivilege(dgvPrivilege.SelectedRows(0).Index)

            End If
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        SetScreenMode(eScreenMode.Add)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        SetScreenMode(eScreenMode.Edit)
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If m_ManagePrivilegeControl.verifyDeletePrivilege() Then
            m_ManagePrivilegeControl.showDeleteResult(m_Privilege.deletePrivilege(txtPrivilegeID.Text))

            ConnectionString.ShowLoadingForm()
            Me.Invoke(New delLoadDatagridviewPrivilege(AddressOf LoadDatagridviewPrivilege), 0)
            ConnectionString.TerminateLoadingForm()

            'LoadDatagridviewPrivilege()
        End If
    End Sub

    Private Sub dgvPrivilege_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrivilege.RowEnter
        LoadDataForm(dgvPrivilege.Rows(e.RowIndex).Cells("PrivilegeID").Value)
    End Sub
#End Region

End Class